<?php
require APP_PATH.'Admin/Model/CategoryModel.class.php';
class IndexController extends Controller{
	public $catDb;
	public function __init(){
		//分配栏目
		$this->catDb = new CategoryModel;
		$category = $this->catDb->getAll();
		foreach($category as $id=>$cat){
			if(isset($_GET['cid']) && $_GET['cid']==$cat['cid']){
				$category[$id]['active']=" class='active' ";
			}else{
				$category[$id]['active']='';
			}
		}
		$this->assign('category',$category);
		//获得最新BLOG
		$db = new Model('blog_blog');
		$sql = "select * from blog_blog order by bid desc limit 10";
		$this->assign('NewBlog',$db->query($sql));
		//热门BLOG
		$sql = "select * from blog_blog order by click desc limit 10";
		$this->assign('HotBlog',$db->query($sql));
	}
	//测试方法
	public function calssify(){
		if (isset($_GET['cid'])) {
			$where=" WHERE cid=".$_GET['cid'];
		}else{
			$where="";
		}
		$count = $this->catDb->query("select count(*) as c from blog_blog $where");
		$page= new page($count[0]['c'],5);
		$sql="select * from blog_blog $where order by bid desc limit ".$page->limit();
		$data=$this->catDb->query($sql);
		// P($data);
		$this->assign('data',$data);
		$this->assign('page',$page->show());
		$this->display('newlist.html');

	}
	public function index(){
		//如果有cid参数,只获取该cid的文章
		if(isset($_GET['c.cid'])){
			$where=" WHERE  c.cid=".$_GET['c.cid'];
		}else{
			$where='';
		}
		$db = new Model('blog_blog');
		$count = $db->query("select count(*) as c from blog_blog $where");
		$page= new Page($count[0]['c'],5);
		// if (isset($_GET['cid'])) {
		// 	$cid=$_GET['cid'];
		// 	$m=new Model('blog_category');
		// 	$data = $m->where("cid = {$_GET['cid']}")->find();
		// 	$catName = $data['catname'];
		// 	$this->assign('cat',$catName);
		// }
		// 获得相应的分类名称
		// if(isset($_GET['cid'])){
		// 	$cid = $_GET['cid'];
		// switch ($cid) {
		// 	case $_GET['cid']:
		// 		// 实例化一个对象
		// 		$m = new Model('blog_category');
		// 		// 获取相对应的一条数据
		// 		$data = $m->where("cid = {$_GET['cid']}")->find();
		// 		// 把数据里面的分类名称保存到变量里面
		// 		$catName = $data['catname'];
		// 		// 把分类名称分配给smarty变量
		// 		$this->assign('cat',$catName);
		// 		break;
		// }
		// // $this->display('newlist.html');
		// }
		// 获得相应的分类名称
		// 查找数据
		$sql = "select * from blog_blog b join blog_category c on b.cid=c.cid  {$where} order by bid desc limit ".$page->limit();
		$data= $db->query($sql);
		$this->assign('data',$data);
		$this->assign('page',$page->show());
		$this->display('index.html');
	}
	//博客详情
	public function blog(){
		$bid = $_GET['bid'];
		$db = new Model('blog_blog');
		$sql = "select * from blog_blog b join blog_category c on b.cid=c.cid where bid=$bid";
		$field =$db->query($sql);
		$this->assign('field',$field[0]);
		$this->display('blog.html');
	}
	//获得点击数
	public function getClick(){
		$bid =$_GET['bid'];
		//更新点击数
		$db = new Model('blog_blog');
		$sql = "UPDATE blog_blog set click=click+1 where bid=$bid";
		$db->exe($sql);
		$field = $db->where("bid=$bid")->find();
		echo "document.write({$field['click']})";
	}
}